package net.wanho.productManage.controller;

import net.wanho.commons.exception.ServiceException;
import net.wanho.commons.factory.BeanFactory;
import net.wanho.commons.servlet.BaseServlet;
import net.wanho.commons.util.IDUtil;
import net.wanho.customerManage.po.Customer;
import net.wanho.customerManage.service.CustomerService;
import net.wanho.productManage.po.Orders;
import net.wanho.productManage.po.OrdersDetail;
import net.wanho.productManage.po.Product;
import net.wanho.productManage.po.Store;
import net.wanho.productManage.service.OrdersService;
import net.wanho.productManage.service.ProductService;
import net.wanho.productManage.service.StoreService;
import net.wanho.systemSettings.po.Employee;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@WebServlet("/OrderServlet")
public class OrderServlet extends BaseServlet<OrdersService> {
    private CustomerService customerService = BeanFactory.getBean(CustomerService.class) ;
    private ProductService productService = BeanFactory.getBean(ProductService.class) ;
    private StoreService storeService = BeanFactory.getBean(StoreService.class) ;
    /**
     * 查询所有 订单列表
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String allOrders(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
        try {
            List<Orders> ordersList = super.baseService.findAll();
            request.setAttribute("ordersList",ordersList);
            return "/view/productManage/sales/index.jsp" ;
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage()) ;
        }
    }

    //跳转到新增销售单页面
    public String toCreateOrderView(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
        try {
            //1. 查询所有顾客
            List<Customer> customerList = customerService.findAll();
            //2. 查询所有商品
            List<Product> productList = productService.findAll();
            //3. 查询所有仓库
            List<Store> storeList = storeService.findAll();
            //4. 向订单表插入一条数据 【订单生成中...】
            Orders orders = new Orders();
            orders.setOrderSeq(IDUtil.genOrderSeq());
            Employee employee = (Employee) request.getSession().getAttribute("currEmployee");
            orders.setCreateTime(new Date());
            orders.setOperator(employee);
            orders.setOrderType("XS");
            orders.setStatus("0");
            orders.setTotalMoney(BigDecimal.ZERO);
            super.baseService.save(orders) ;
            //数据设置到作用域中
            request.setAttribute("customerList",customerList);
            request.setAttribute("productList",productList);
            request.setAttribute("storeList",storeList);
            request.setAttribute("orders",orders);
            return "/view/productManage/sales/create.jsp" ;
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage()) ;
        }
    }


    //提交订单
    public String submitOrders(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
        try {
            int orderDetailSize = Integer.parseInt(request.getParameter("orderDetailSize"));
            //关联的 list 属性赋值
            Orders orders = new Orders() ;
            List<OrdersDetail> ordersDetailList = new ArrayList<>() ;
            for (int i = 0; i < orderDetailSize; i++) {
                ordersDetailList.add(new OrdersDetail()) ;
            }
            //设置关联
            orders.setOrdersDetailList(ordersDetailList);
            super.parseRequest(request, orders);
            super.baseService.submitOrder(orders) ;
            return "redirect:/OrderServlet?method=allOrders" ;
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage()) ;
        }
    }

    //审核订单【商品状态  商品价格  商品 小计 总计】
    public String auditOrders(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
        try {
            String orderSeq = request.getParameter("orderSeq");
            super.baseService.auditOrders(orderSeq) ;
            return "redirect:/OrderServlet?method=allOrders" ;
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage()) ;
        }
    }

    //出库
    public String outOfStock(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
        try {
            String orderSeq = request.getParameter("orderSeq");
            super.baseService.outOfStock(orderSeq) ;
            return "redirect:/OrderServlet?method=allOrders" ;
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage()) ;
        }
    }
}
